﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
	public class IPRutaFabricacion : Intermediario
	{
		public override void save<T>(T entity)
		{
			try
			{
				RutaFabricacion rf = entity as RutaFabricacion;
				dao.RutaFabricacions.InsertOnSubmit(rf);
			}
			catch (Exception ex)
			{
				throw new Exception("IPRProveedor.save - " + ex.Message);
			}
		}

		public override List<T> getAll<T>()
		{
			return (from rf in dao.RutaFabricacions orderby rf.Producto.NombreProducto select rf).ToList() as List<T>;
		}

		public override List<T> getByCriterio<T>(string att, string op, string val)
		{
			return (from rf in dao.RutaFabricacions where rf.Producto.NombreProducto.Contains(val) orderby rf.Producto.NombreProducto select rf).ToList() as List<T>;
		}

		public override IList<T> getCriterioById<T>(string att, string op, int val)
		{
			return (from rf in dao.RutaFabricacions where rf.IdProducto == val && rf.Activo == true select rf).ToList() as List<T>;
		}

		public override object getPorId(int id)
		{
			IList<RutaFabricacion> rutas = (from rf in dao.RutaFabricacions where rf.IdRF == id select rf).ToList();

			return (rutas.Count > 0 ? rutas.First() : null);
		}

		public override IList<T> getPorCriterio<T>(string name)
		{
			throw new NotImplementedException();
		}

		public override void Update<T>(T entity)
		{
			throw new NotImplementedException();
		}

		public override void Delete(int id)
		{
			RutaFabricacion ruta = (RutaFabricacion)getPorId(id);
			dao.RutaFabricacions.DeleteOnSubmit(ruta);
		}

		public void DeleteRF_CT(IList<RF_CT> rutasABorrar)
		{
			dao.RF_CTs.DeleteAllOnSubmit(rutasABorrar);
		}

        public IList<RF_CT> GetRF_CT(int i)
        {
            return (from rc in dao.RF_CTs where rc.IdRF == i select rc).ToList();
        }
	}
}
